package org.algorithm.wE1.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;
import org.junit.Test;

/**
 * @Date 2022/2/26
 * @Link {https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii}
 */
public class 删除排序链表中的重复元素II {



    @Test
    public  void test(){

        ListNode listNode = deleteDuplicates(ListNode.build(1, 2, 3, 3, 4, 4, 5));
        System.out.println(ListNode.print(listNode));

    }

    public ListNode deleteDuplicates(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode.next;
        while (pre.next!=null&&pre.next.next!=null){
            if(pre.next.val == pre.next.next.val){
                int val = pre.next.next.val;
                while (pre.next!=null&&pre.next.val == val){
                    pre.next = pre.next.next;
                }
            }else {
                pre = pre.next;
            }
        }
        return dummyNode.next;
    }
}
